package nl.leiden.activities;

import java.io.File;
import java.util.ArrayList;

import nl.leiden.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
import android.view.View;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;

import android.gesture.Gesture;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;

public class ReviewActivity extends Activity implements OnGesturePerformedListener {
	
	//----------------------------------------------------
	private GestureLibrary gesturesLibActions;
    
    private StringBuffer sBufferGestureText = new StringBuffer();
    private String sGestureText = new String();

    //----------------------------------------------------
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_review);
        
        //--buttons
        /*Button previous = (Button) findViewById(R.id.ButtonReviewPrevious);
        previous.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), WritingActivity.class);
                startActivityForResult(myIntent, 0);
            }

        });

        Button next = (Button) findViewById(R.id.ButtonReviewNext);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), SendActivity.class);
                startActivityForResult(myIntent, 0);
            }
            
        });*/
        
		Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
		long[] pattern = {0, 200, 200, 200, 200, 200};
		v.vibrate(pattern, -1);
        
        //-- load gesture libraries
        gesturesLibActions = GestureLibraries.fromRawResource(this, R.raw.gestures_lib_actions);
        if(!this.gesturesLibActions.load()) {
        	this.finish();
        }
        
        //-- get gesture overlay view to add on gesture performed listener
        GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
        gestures.addOnGesturePerformedListener(this);
        
        //-- get sMessage from preferences
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        String sMessage = preferences.getString("sMessage", "");
        
        //-- reset buffer with content from preferences
        this.sBufferGestureText.delete(0, this.sBufferGestureText.length());
        this.sBufferGestureText.append( sMessage );
        this.sGestureText = sBufferGestureText.toString();
        
        //-- set sMessage to TextView on Screen
        TextView tv = (TextView) findViewById(R.id.ReviewMessageTextView);
        tv.setText(sMessage);
        
    }
    
    //----------------------------------------------------
    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
    	//--recognize libraries
    	ArrayList<Prediction> predictionsActions = gesturesLibActions.recognize(gesture);

		// We want at least one prediction
		if (predictionsActions.size() > 0) {
			Prediction prediction = predictionsActions.get(0);
			// We want at least some confidence in the result
			if (prediction.score > 1.5) {
				
				//--action name
				String action = predictionsActions.get(0).name;
				
				//--action: confirm
				if ("action_confirm".equals(action)) {				
					Intent myIntent = new Intent(this,SendActivity.class);
	                startActivityForResult(myIntent, 0);
				}
				
				//--action: goback
				else if ("action_goback".equals(action)) {	
					Intent myIntent = new Intent(this, WritingActivity.class);
	                this.startActivity(myIntent);
				}

			} else {
				Toast.makeText(this, "gesture not recognized", Toast.LENGTH_SHORT).show();
			}
		}
	}
}